import { getLocal, setLocal } from "./local";
/**
 * 每次打开的窗口都会存储在 浏览器存储里面 这里可以获取打开的窗口信息
 * @returns 
 */

export const getFolders = () => {
  if (getLocal("windows-folders")) return getLocal("windows-folders");
  return [];
}
/**
 * 设置窗口的名称 icon 地址信息
 * @param 窗口名称 title 
 * @param 窗口icon icon 
 * @param 链接地址 path 
 * @returns 
 */
export const setFolder = (title, icon, path) => {
  if (getLocal("windows-folders")) {
    const folders = getLocal("windows-folders");
    const res = folders.find((item) => item.title === title)
    if (res) {
      updateShowFolder(res.path);
      return false
    }
    folders.push({
      title,
      icon,
      path,
      fullScreen: false,
      show: true,
    });
    setLocal("windows-folders", folders);
    return true;
  } else {
    const folders = [];
    folders.push({
      title,
      icon,
      path,
      fullScreen: false,
      show: true,
    });
    setLocal(("windows-folders"), folders);
    return true;
  }
}
/**
 * 用来处理localStorage中存储的窗口信息
 * @param {传入关闭窗口信息} folderName 
 */
export const closeFolder = (folderName) => {
  if (getLocal("windows-folders")) {
    const folders = getLocal("windows-folders").filter(
      (folder) => folder.path !== folderName
    )
    setLocal("windows-folders", folders);
  }
}

// 最小画或展示窗口逻辑
export const updateShowFolder = (foldername) => {
  if (getLocal("windows-folders")) {
    const lastFolders = getLocal("windows-folders");
    const folders = [];
    lastFolders.map((folder) => {
      if (folder.path === foldername) {
        folders.push({
          title: folder.title,
          icon: folder.icon,
          path: folder.path,
          fullScreen: folder.fullScreen,
          show: !folder.show,
        })
      } else {
        folders.push(folder)
      }
    })
    setLocal("windows-folders", folders);
  }
}

// 窗口是否全屏
export const updateFullScreen = (folderName) => {
  if (getLocal("windows-folders")) {
    const lastFolders = getLocal("windows-folders");
    const folders = [];
    lastFolders.map((folder) => {
      if (folder.path === folderName) {
        folders.push({
          title: folder.title,
          icon: folder.icon,
          path: folder.path,
          show: folder.show,
          fullScreen: !folder.fullScreen,
        });
      } else {
        folders.push(folder);
      }
    });
    setLocal("windows-folders", folders);
  }
}